package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.MemberOfQueryDTO;
import com.jeff.tianti.cms.entity.Article;
import com.jeff.tianti.cms.entity.MemberOf;
import com.jeff.tianti.cms.service.MemberOfService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

//理事会员数据操作
@Controller
@RequestMapping("/member")
public class MemberOfController {
    @Autowired
    private MemberOfService memberOfService;

    /**
     *@Ceator:liuYI
     *@Describe:添加理事会员
    */
    @RequestMapping("/addMemberOf")
    @ResponseBody
    public AjaxResult addMemberOf(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String areaId = request.getParameter("areaId");//地区id
            String memberType = request.getParameter("memberType");//理事会员类型:0->常务副会长,1->副会长,2->常务理事,3->理事,4->集体会员,5->个人会员
            String companyName = request.getParameter("companyName");//公司名称
            String linkman = request.getParameter("linkman");//联系人
            String phone = request.getParameter("phone");//联系电话
            String money = request.getParameter("money");//会费缴纳
            String deleteFlag = request.getParameter("deleteFlag");//是否前台展示   后台永远可编辑   0->封存；1->不封存
            String eMail = request.getParameter("eMail");//网页邮箱
            MemberOf memberOf = null;
            if (StringUtils.isNotBlank(id)) {
                memberOf = memberOfService.find(id);
            } else {
                memberOf = new MemberOf();
            }
            memberOf.setAreaId(StringUtils.trim(areaId));
            memberOf.setMemberType(StringUtils.trim(memberType));
            memberOf.setCompanyName(StringUtils.trim(companyName));
            memberOf.setLinkman(StringUtils.trim(linkman));
            memberOf.setPhone(StringUtils.trim(phone));
            BigDecimal bigDecima = null;
            try {
                bigDecima = new BigDecimal(money);
            } catch (Exception e) {
                e.printStackTrace();
            }
            memberOf.setMoney(bigDecima);
            memberOf.setDeleteFlag(StringUtils.trim(deleteFlag));
            memberOf.seteMail(StringUtils.trim(eMail));
            memberOfService.saveMemberOf(memberOf);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     *@Ceator:liuYI
     *@Describe:数据回显
     */
    @RequestMapping("/findById")
    public String findById(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            MemberOf memberOf = memberOfService.find(id);
            model.addAttribute("memberOf", memberOf);
        }
        return "user/member/member_edit";
    }
    /**
     *@Ceator:liuYI
     *@Describe:获取会员列表
     */
    @RequestMapping("/list")
    public String memberList(HttpServletRequest request, Model model) {
        String level = request.getParameter("level");//理事会员类型:0->常务副会长,1->副会长,2->常务理事,3->理事,4->集体会员,5->个人会员
        String companyName = request.getParameter("companyName");//公司名称
        String phone = request.getParameter("phone");//手机号
        String currentPageStr = request.getParameter("currentPage");//当前页
        String pageSizeStr = request.getParameter("pageSize");//每页显示条数
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        MemberOfQueryDTO memberOfQueryDTO = new MemberOfQueryDTO();
        memberOfQueryDTO.setPageSize(pageSize);
        memberOfQueryDTO.setCurrentPage(currentPage);
        memberOfQueryDTO.setLevel(level);
        memberOfQueryDTO.setCompanyName(companyName);
        memberOfQueryDTO.setPhone(phone);
        PageModel<MemberOf> page = memberOfService.queryMemberOfPage(memberOfQueryDTO);
        model.addAttribute("page", page);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("memberOfQueryDTO", memberOfQueryDTO);
        return "user/member/member_list";
    }
    /**
     *@Ceator:liuYI
     *@Describe:删除
     */
    @ResponseBody
    @RequestMapping("/deleteById")
    public AjaxResult deleteById(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String[] ids = request.getParameterValues("ids");
            String deleteFlag = request.getParameter("deleteFlag");
            if(ids != null && ids.length > 0){
                for(String id:ids){
                    MemberOf memberOf = memberOfService.find(id);
                    memberOf.setDeleteFlag(deleteFlag);
                    memberOfService.update(memberOf);
                }
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;

    }
}
